import { FileService } from '@app/file';
import { HashService } from '@app/hash';
import { PrismaService } from '@app/prisma';
import { GoodsEntity } from '@app/prisma/entitys/goods.entity';
import { ShopEntity } from '@app/prisma/entitys/shop.entity';
import { UserEntity } from '@app/prisma/entitys/user.entity';
import { UseGuards } from '@nestjs/common';
import { Args, Int, Mutation, Query, Resolver } from '@nestjs/graphql';
import { Prisma } from '@prisma/client';
import { CurrentUser, EditGoodsPower, GqlAuthGuard, GqlAuthPowerGuard, GqlCurrentUser } from 'src/auth/auth.guard';

@Resolver()
export class GoodsResolver {
    constructor(
        private readonly prismaService: PrismaService,
        private readonly fileService: FileService,
        private readonly hashService: HashService
    ) { }

    @Query(() => ShopEntity, { nullable: true })
    @UseGuards(GqlAuthGuard)
    getSelfShopDetial(
        @GqlCurrentUser() user: UserEntity
    ) {
        return this.prismaService.shop.findUnique({ where: { phone: user.account } })
    }

    @Mutation(() => GoodsEntity)
    @UseGuards(GqlAuthGuard)
    addGoods(
        @Args("avatar") avatar: string,
        @Args("name") name: string,
        @Args("information", { nullable: true }) information: string,
        @Args("price", { type: () => Int }) price: number,
        @Args("status", { type: () => Int }) status: boolean,
        @Args("menu") menu: string,
        @Args("shopId", { type: () => Int }) shopId: number
    ) {
        const hash_key = this.hashService.createUid()
        this.fileService.fileW2R(this.fileService.getGoodsAvatar(hash_key), Buffer.from(avatar, "base64"))
        return this.prismaService.goods.create({
            data: {
                name,
                price,
                status: !!status,
                menu,
                hash_key,
                information,
                shop: {
                    connect: {
                        id: shopId
                    }
                }
            }
        })
    }

    @UseGuards(GqlAuthGuard)
    @Mutation(() => GoodsEntity)
    async updGoods(
        @Args("avatar", { nullable: true }) avatar: string,
        @Args("name") name: string,
        @Args("information", { nullable: true }) information: string,
        @Args("price", { type: () => Int }) price: number,
        @Args("status", { type: () => Int }) status: boolean,
        @Args("menu") menu: string,
        @Args("id", { type: () => Int }) id: number,
        @Args("shopId", { type: () => Int, nullable: true }) shopId?: number,
    ) {
        const { hash_key } = await this.prismaService.goods.findUniqueOrThrow({ where: { id } });
        if (!!avatar) this.fileService.fileW2R(this.fileService.getGoodsAvatar(hash_key), Buffer.from(avatar, "base64"))
        const data: Prisma.GoodsUpdateInput = {
            name,
            price,
            status: !!status,
            menu,
            information
        };
        if (!!shopId) {
            data.shop = {
                connect: {
                    id: shopId
                }
            }
        }
        return this.prismaService.goods.update({
            where: {
                id
            },
            data
        })
    }


    @UseGuards(GqlAuthGuard)
    @Mutation(() => GoodsEntity)
    delGoods(
        @Args("id", { type: () => Int }) id: number
    ) {
        return this.prismaService.goods.delete({
            where: {
                id
            }
        })
    }

    @Query(() => [GoodsEntity])
    @UseGuards(GqlAuthGuard)
    getSelfGoods(
        @GqlCurrentUser() user: UserEntity
    ) {
        return this.prismaService.goods.findMany({
            where: {
                shop: {
                    phone: user.account
                }
            },
            orderBy: {
                id: "desc"
            }
        })
    }
    @Query(() => [GoodsEntity])
    @UseGuards(GqlAuthGuard)
    getAllGoods() {
        return this.prismaService.goods.findMany({
            orderBy: {
                id: "desc"
            },
            include: {
                shop: true
            }
        })
    }
}
